<?php
	include_once "dataservice.php";
	include_once "config.php";
	$ds = new dataservice();
	
	$CF = new Config();
	
	$DatabaseName = $CF->Database;
	
	$sql = "SELECT table_name
			FROM information_schema.tables
			WHERE table_schema =  '" . $DatabaseName . "'
			ORDER BY table_name ASC";
	$list_table = $ds->ExecuteQuery($sql);
	//Build file 
	for($i = 0; $i < count($list_table); $i++)
	{
		$tenfile_arr = explode("_", $list_table[$i][0]);
		$tenfile = "";
		for($j = 1; $j < count($tenfile_arr); $j++)
			$tenfile .= $tenfile_arr[$j];
		//Mở file dựa vào tên bảng
		// file *db.php
		$fp = fopen("source/data_layer/" . $tenfile . "_db.php", "a+");
		$tmp = "<?php \n\tinclude_once 'dataservice.php';\n\tinclude_once 'thuvienham.php';\n\n\tclass " . $tenfile . "_db \n\t{";
		
		fwrite($fp, $tmp);
		
		$sql = "SELECT column_name
				FROM information_schema.columns
				WHERE table_name =  '" . $list_table[$i][0] . "'";
				
		$list_field = $ds->ExecuteQuery($sql);
		
		$sql = "SHOW COLUMNS FROM " . $list_table[$i][0];
		$list_field_type = $ds->ExecuteQuery($sql);
		
		$BienHam      = "";
		$TenField     = "";
		$ValuesInsert = "";
		$SetCapNhat   = "";
		$WhereCapNhat = "";
		$BienXoa      = "";
		$DieuKienXoa  = "";
		for($j = 0; $j < count($list_field); $j++)
		{
			if($j > 0)
			{
				$BienHam      .= ", ";
				$TenField     .= ", ";
				$ValuesInsert .= ", ";
				$SetCapNhat   .= "`" . $list_field[$j][0] . "` = " . "'\$" . $list_field[$j][0] . "'";
				if($j < count($list_field) - 1)
					$SetCapNhat .= ", ";
			}
			else
			{
				$WhereCapNhat .= "`" . $list_field[$j][0] . "` = " . "'\$" . $list_field[$j][0] . "'";
				$BienXoa      .= "\$" . $list_field[$j][0];
				$DieuKienXoa  .= "`" . $list_field[$j][0] . "` = " . "'\$" . $list_field[$j][0] . "'";
			}
			$BienHam      .= "\$" . $list_field[$j][0];
			$TenField     .= "`" . $list_field[$j][0] . "`";
			$ValuesInsert .= "'\$" . $list_field[$j][0] . "'";
		}
		
		//Hàm Them
		$tmp = 	"\n\t\tpublic function Them(" . $BienHam . ")\n\t\t{".
				"\n\t\t\t\$sql = \"INSERT INTO `" . $list_table[$i][0] . "`(" . $TenField . ")".
				"\n\t\t\t\t\tVALUES (" . $ValuesInsert . ")\";".
				"\n".
				"\n\t\t\t\$service = new dataservice();".
				"\n\t\t\treturn \$service->ExecuteNonQuery(\$sql);".
				"\n\t\t}\n";
		fwrite($fp, $tmp);
		
		//Hàm Xoa
		$tmp = 	"\n\t\tpublic function Xoa(" . $BienXoa . ")\n\t\t{".
				"\n\t\t\t\$sql = \"DELETE FROM `" . $list_table[$i][0] . "` WHERE " . $DieuKienXoa . "\";".
				"\n".
				"\n\t\t\t\$service = new dataservice();".
				"\n\t\t\treturn \$service->ExecuteNonQuery(\$sql);".
				"\n\t\t}\n";
		fwrite($fp, $tmp);
		
		//Hàm CapNhat
		$tmp = 	"\n\t\tpublic function CapNhat(" . $BienHam . ")\n\t\t{".
				"\n\t\t\t\$sql = \"UPDATE `" . $list_table[$i][0] . "`" .
				"\n\t\t\t\t\tSET " . $SetCapNhat . 
				"\n\t\t\t\t\tWHERE " . $WhereCapNhat . "\";" . 
				"\n".
				"\n\t\t\t\$service = new dataservice();".
				"\n\t\t\treturn \$service->ExecuteNonQuery(\$sql);".
				"\n\t\t}\n";
		fwrite($fp, $tmp);
		
		//Hàm LayDanhSach()
		$tmp = 	"\n\t\tpublic function LayDanhSach(" . $BienHam . ")\n\t\t{".
				"\n\t\t\t\$sql = \"SELECT * FROM `" . $list_table[$i][0] . "` WHERE 1  \";";
				fwrite($fp, $tmp);
		for($j = 0; $j < count($list_field_type); $j++)
		{
			$BienIf = "\$" . $list_field_type[$j]['Field'];
			
			//Set biến tạm về kiểu dữ liệu - DataType = 
			//1: int, double - 2: varchar, string, text - 3: date - 4: datetime
			$tBool_1 = strpos($list_field_type[$j]['Type'], "int") !== false || strpos($list_field_type[$j]['Type'], "double") !== false;
			$tBool_2 = strpos($list_field_type[$j]['Type'], "char") !== false || strpos($list_field_type[$j]['Type'], "text") !== false;
			$tBool_3 = strpos(strtolower($list_field_type[$j]['Type']), "date") !== false;
			$tBool_4 = strpos($list_field_type[$j]['Type'], "datetime") !== false;
			
			if($tBool_1)
				$DieuKienIf = "\$" . $list_field_type[$j][0] . " != -1";
			elseif($tBool_2)
				$DieuKienIf = "\$" . $list_field_type[$j][0] . " != \"\"";
			elseif($tBool_3 || $tBool_4)
			{
				if($tBool_4)
					$DieuKienIf = "\$" . $list_field_type[$j][0] . " != \"0000-00-00 00:00:00\"";
				else
					$DieuKienIf = "\$" . $list_field_type[$j][0] . " != \"0000-00-00\"";
			}
			else
				$DieuKienIf = "\$" . $list_field_type[$j][0] . " != \"\"";
			$tmp =	"\n\t\t\tif(" . $DieuKienIf . ")" . 
					"\n\t\t\t\t\$sql .= \" AND `" . $list_field_type[$j][0] . "` = '\$" . $list_field_type[$j][0] . "' \";" . 
					"\n";
					fwrite($fp, $tmp);
		}		
		$tmp =	"\n\t\t\t\$service = new dataservice();".
				"\n\t\t\treturn \$service->ExecuteQuery(\$sql);".
				"\n\t\t}\n";
		fwrite($fp, $tmp);
		
		$tmp = "\n\t}" .
		"\n\n";
		//Kết thúc file ?\>
		fwrite($fp, $tmp);
		fclose($fp);
	}
	
	echo "<script> alert('Đã tạo xong " . count($list_table) . " file của lớp Data');</script>";
?>